<?php
include 'conn.php';
$conn = new \AMQPConnection($conn_args);
if (!$conn->connect()) {
     die("Cannot connect to the broker!\n");
}
$channel = new \AMQPChannel($conn);

//创建交换机

$ex = new AMQPExchange($channel);
$ex->setName($e_name);

$ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型

$ex->setFlags(AMQP_DURABLE); //持久化

echo "Exchange Status:".$ex->declareExchange().PHP_EOL;

//创建队列
$q= new AMQPQueue($channel);
$q->setName($q_name);
$q->setFlags(AMQP_DURABLE);
echo "Message total:".$q->declareQueue().PHP_EOL;


//绑定交换机与队列，并制定路由键名

echo "Queue Bind:".$q->bind($e_name,$k_route).PHP_EOL;




while($AMQPEnvelope=$q->get()){
    echo "-------------------------------------------------------------------".PHP_EOL;
    echo "Message total:".$q->declareQueue().PHP_EOL;
    echo "getBody:". $AMQPEnvelope->getBody().PHP_EOL;
    echo "getRoutingKey:".$AMQPEnvelope->getRoutingKey().PHP_EOL;
    echo "getConsumerTag:".$AMQPEnvelope->getConsumerTag().PHP_EOL;
    echo "getExchangeName:".$AMQPEnvelope->getExchangeName().PHP_EOL;
    echo "isRedelivery:".$AMQPEnvelope->isRedelivery().PHP_EOL;
    echo "getDeliveryTag:".$AMQPEnvelope->getDeliveryTag().PHP_EOL;
//echo "getHeader:".$AMQPEnvelope->getHeader().PHP_EOL;
//echo "hasHeader:".$AMQPEnvelope->hasHeader().PHP_EOL;
    echo "getContentType:".$AMQPEnvelope->getContentType().PHP_EOL;
    echo "getMessageId:".$AMQPEnvelope->getMessageId().PHP_EOL;
    echo "getTimestamp:".$AMQPEnvelope->getTimestamp().PHP_EOL;
    echo "getType:".$AMQPEnvelope->getType().PHP_EOL;
    echo "getUserId:".$AMQPEnvelope->getUserId().PHP_EOL;
    echo "getAppId:".$AMQPEnvelope->getAppId().PHP_EOL;
    $q->ack($AMQPEnvelope->getDeliveryTag());
}




/*while(1){
    $q->consume("processMessage");

//    var_dump($msg);
    //$q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答
}*/

$conn->disconnect();

function processMessage($envelope,$queue){
    $msg=$envelope->getBody();

    echo $msg.PHP_EOL;//处理消息
    $queue->ack($envelope->getDeliveryTag());////手动发送ACK应答
}



















































